package top.mataiji.manager.controller;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;
import mataiji.config.util.Result;
import top.mataiji.manager.pojo.dtd.OrderDtd;
import top.mataiji.manager.pojo.vo.OrderVo;
import top.mataiji.manager.service.OrderService;
import top.mataiji.manager.service.*;
import top.mataiji.manager.service.CartService;

import java.util.List;

/**
 * @Auther: zhaoss
 * @Date: 2021/3/24 - 03 - 24 - 13:52
 * @Description: top.mataiji.manager.controller
 * @version: 1.0
 */
@RestController
@RequestMapping("order")
public class OrserController {

    @Autowired
    OrderService orderService;
    @Autowired
    CartService cartServcie;

    /**
     * 添加订单
     * @param orderDtd
     * @return
     * @throws Exception
     */
    @Transactional(rollbackFor = Exception.class)
    @RequestMapping("addOrder")
    @ResponseBody
    public Result placeAnOrder(OrderDtd orderDtd) throws Exception{
//        System.out.println(orderDtd);
        //将orderDtd封装为orderVo对象
        String i = orderService.addOrder(orderDtd);
        System.out.println(i);
        if(i!=null) {
            if(orderDtd.getTeid()!=null&&!orderDtd.getTeid().isEmpty()){
                orderDtd.getTeid().forEach(e->{
                    cartServcie.deleteCartById(e);
                });
            }
            return Result.success("下单成功",i);
        } else {
            return Result.error("下单失败");
        }

    }

    /**
     * 查询订单
     * @param userId
     * @return
     */
    @RequestMapping("findAllOrder")
    public List<OrderVo> findAllOrder(Integer userId){
        return orderService.findAllOrder(userId);
    }

}
